package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.bean.ResponseBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.mvp.presenter.InsertCurriculumPresenter;
import com.android.infantschool.model.mvp.presenter.InsertCurriculumPresenterImpl;
import com.android.infantschool.model.mvp.view.InsertCurriculumView;
import com.android.infantschool.util.DateUtil;
import com.android.infantschool.util.EdittorUtil;
import com.android.infantschool.util.ToastUtil;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;

import org.json.JSONArray;
import org.json.JSONException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\20 0020 15:32
 * @class describe
 */
public class WeekCurriculumActivity extends BaseActivity implements InsertCurriculumView {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.ed_forenoon_1)
    public EditText edForenoon1;
    @BindView(R.id.ed_afternoon_1)
    public EditText edAfternoon1;
    @BindView(R.id.ed_forenoon_2)
    public EditText edForenoon2;
    @BindView(R.id.ed_afternoon_2)
    public EditText edAfternoon2;
    @BindView(R.id.ed_forenoon_3)
    public EditText edForenoon3;
    @BindView(R.id.ed_afternoon_3)
    public EditText edAfternoon3;
    @BindView(R.id.ed_forenoon_4)
    public EditText edForenoon4;
    @BindView(R.id.ed_afternoon_4)
    public EditText edAfternoon4;
    @BindView(R.id.ed_forenoon_5)
    public EditText edForenoon5;
    @BindView(R.id.ed_afternoon_5)
    public EditText edAfternoon5;
    @BindView(R.id.btn_release)
    public Button btnRelease;
    @BindView(R.id.ed_morning_1)
    public EditText edMorning1;
    @BindView(R.id.ed_morning_2)
    public EditText edMorning2;
    @BindView(R.id.ed_morning_3)
    public EditText edMorning3;
    @BindView(R.id.ed_morning_4)
    public EditText edMorning4;
    @BindView(R.id.ed_morning_5)
    public EditText edMorning5;
    @BindView(R.id.tv_week_time_1)
    public TextView tvWeekTime1;
    @BindView(R.id.tv_week_time_2)
    public TextView tvWeekTime2;
    @BindView(R.id.tv_week_time_3)
    public TextView tvWeekTime3;
    @BindView(R.id.tv_week_time_4)
    public TextView tvWeekTime4;
    @BindView(R.id.tv_week_time_5)
    public TextView tvWeekTime5;
    @BindView(R.id.tv_week_time_6)
    public TextView tvWeekTime6;
    @BindView(R.id.ed_morning_6)
    public EditText edMorning6;
    @BindView(R.id.ed_forenoon_6)
    public EditText edForenoon6;
    @BindView(R.id.ed_afternoon_6)
    public EditText edAfternoon6;
    @BindView(R.id.tv_week_time_7)
    public TextView tvWeekTime7;
    @BindView(R.id.ed_morning_7)
    public EditText edMorning7;
    @BindView(R.id.ed_forenoon_7)
    public EditText edForenoon7;
    @BindView(R.id.ed_afternoon_7)
    public EditText edAfternoon7;

    public static Intent newIntent(Activity activity) {
        Intent intent = new Intent(activity, WeekCurriculumActivity.class);
        return intent;
    }

    private InsertCurriculumPresenter curriculumPresenter = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_week_curriculum);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        curriculumPresenter = new InsertCurriculumPresenterImpl(this);
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText("新增计划");
        tvWeekTime1.setText(DateUtil.getWeekEnd(0));
        tvWeekTime2.setText(DateUtil.getWeekEnd(1));
        tvWeekTime3.setText(DateUtil.getWeekEnd(2));
        tvWeekTime4.setText(DateUtil.getWeekEnd(3));
        tvWeekTime5.setText(DateUtil.getWeekEnd(4));
        tvWeekTime6.setText(DateUtil.getWeekEnd(5));
        tvWeekTime7.setText(DateUtil.getWeekEnd(6));
    }

    @OnClick({R.id.img_back, R.id.btn_release, R.id.tv_week_time_1, R.id.tv_week_time_2, R.id.tv_week_time_3, R.id.tv_week_time_4, R.id.tv_week_time_5, R.id.tv_week_time_6, R.id.tv_week_time_7})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
            case R.id.btn_release:
                String edZao1 = edMorning1.getText().toString().replace(" ", "").trim();
                String edZao2 = edMorning2.getText().toString().replace(" ", "").trim();
                String edZao3 = edMorning3.getText().toString().replace(" ", "").trim();
                String edZao4 = edMorning4.getText().toString().replace(" ", "").trim();
                String edZao5 = edMorning5.getText().toString().replace(" ", "").trim();
                String edZao6 = edMorning6.getText().toString().replace(" ", "").trim();
                String edZao7 = edMorning7.getText().toString().replace(" ", "").trim();
                String edShang1 = edForenoon1.getText().toString().replace(" ", "").trim();
                String edShang2 = edForenoon2.getText().toString().replace(" ", "").trim();
                String edShang3 = edForenoon3.getText().toString().replace(" ", "").trim();
                String edShang4 = edForenoon4.getText().toString().replace(" ", "").trim();
                String edShang5 = edForenoon5.getText().toString().replace(" ", "").trim();
                String edShang6 = edForenoon6.getText().toString().replace(" ", "").trim();
                String edShang7 = edForenoon7.getText().toString().replace(" ", "").trim();
                String edXia1 = edAfternoon1.getText().toString().replace(" ", "").trim();
                String edXia2 = edAfternoon2.getText().toString().replace(" ", "").trim();
                String edXia3 = edAfternoon3.getText().toString().replace(" ", "").trim();
                String edXia4 = edAfternoon4.getText().toString().replace(" ", "").trim();
                String edXia5 = edAfternoon5.getText().toString().replace(" ", "").trim();
                String edXia6 = edAfternoon6.getText().toString().replace(" ", "").trim();
                String edXia7 = edAfternoon7.getText().toString().replace(" ", "").trim();
                if (TextUtils.isEmpty(edZao1) || TextUtils.isEmpty(edZao2) || TextUtils.isEmpty(edZao3) || TextUtils.isEmpty(edZao4) || TextUtils.isEmpty(edZao5) ||
                        TextUtils.isEmpty(edShang1) || TextUtils.isEmpty(edShang2) || TextUtils.isEmpty(edShang3) || TextUtils.isEmpty(edShang4) || TextUtils.isEmpty(edShang5) ||
                        TextUtils.isEmpty(edXia1) || TextUtils.isEmpty(edXia2) || TextUtils.isEmpty(edXia3) || TextUtils.isEmpty(edXia4) || TextUtils.isEmpty(edXia5)) {
                    ToastUtil.showShortToast(this, "请补全课程内容再发布");
                    return;
                }

                JSONArray jsonArray = new JSONArray();
                org.json.JSONObject object = null;
                for (int index = 0; index < 7; index++) {
                    try {
                        object = new org.json.JSONObject();
                        if (index == 0) {
                            object.put("courseTime", tvWeekTime1.getText().toString());//日期
                            object.put("courseName", edZao1);//晨间活动
                            object.put("noonCourse", edShang1);//中午活动
                            object.put("afternoonCourse", edXia1);//下午活动
                        }
                        if (index == 1) {
                            object.put("courseTime", tvWeekTime2.getText().toString());//日期
                            object.put("courseName", edZao2);//晨间活动
                            object.put("noonCourse", edShang2);//中午活动
                            object.put("afternoonCourse", edXia2);//下午活动
                        }
                        if (index == 2) {
                            object.put("courseTime", tvWeekTime3.getText().toString());//日期
                            object.put("courseName", edZao3);//晨间活动
                            object.put("noonCourse", edShang3);//中午活动
                            object.put("afternoonCourse", edXia3);//下午活动
                        }
                        if (index == 3) {
                            object.put("courseTime", tvWeekTime4.getText().toString());//日期
                            object.put("courseName", edZao4);//晨间活动
                            object.put("noonCourse", edShang4);//中午活动
                            object.put("afternoonCourse", edXia4);//下午活动
                        }
                        if (index == 4) {
                            object.put("courseTime", tvWeekTime5.getText().toString());//日期
                            object.put("courseName", edZao5);//晨间活动
                            object.put("noonCourse", edShang5);//中午活动
                            object.put("afternoonCourse", edXia5);//下午活动
                        }
                        if (index == 5) {
                            object.put("courseTime", tvWeekTime6.getText().toString());//日期
                            object.put("courseName", edZao6);//晨间活动
                            object.put("noonCourse", edShang6);//中午活动
                            object.put("afternoonCourse", edXia6);//下午活动
                        }
                        if (index == 6) {
                            object.put("courseTime", tvWeekTime7.getText().toString());//日期
                            object.put("courseName", edZao7);//晨间活动
                            object.put("noonCourse", edShang7);//中午活动
                            object.put("afternoonCourse", edXia7);//下午活动
                        }
                        jsonArray.put(object);
                    } catch (JSONException e) {
                        Log.e(TAG, "e:" + e.toString());
                    }
                }

                Map<String, String> map = new HashMap<>();
                map.put("token", AppConfig.getToken());
                map.put("key", jsonArray.toString().substring(2, jsonArray.toString().length() - 2));
                Log.e(TAG, "map:" + map.toString());
                curriculumPresenter.postInsertCurriculum(map);
                this.showLoading();
                break;
            case R.id.tv_week_time_1:
                showYearMonthPicker(tvWeekTime1);
                break;
            case R.id.tv_week_time_2:
                showYearMonthPicker(tvWeekTime2);
                break;
            case R.id.tv_week_time_3:
                showYearMonthPicker(tvWeekTime3);
                break;
            case R.id.tv_week_time_4:
                showYearMonthPicker(tvWeekTime4);
                break;
            case R.id.tv_week_time_5:
                showYearMonthPicker(tvWeekTime5);
                break;
            case R.id.tv_week_time_6:
                showYearMonthPicker(tvWeekTime6);
                break;
            case R.id.tv_week_time_7:
                showYearMonthPicker(tvWeekTime7);
                break;
        }
    }

    @Override
    public void errorInsertCurriculum(String error) {
        this.dismissLoading();
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successInsertCurriculum(BaseBean baseBean) {
        this.dismissLoading();
        ResponseBean bean = (ResponseBean) baseBean;
        if (bean.getCode() == 0) {
            ToastUtil.showShortToast(this, "提交成功:");
            this.finish();
        } else {
            ToastUtil.showShortToast(this, "提交失败:" + bean.getMsg());
        }
    }

    /**
     * 年月日选择
     */
    private void showYearMonthPicker(final TextView textView) {
        new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = formatter.format(date);
                textView.setText(dateString);
            }
        }).setSubmitColor(0xFF007C70).setCancelColor(0xFF999999).build().show();
    }

    /**
     * 触摸事件
     */
    @Override
    public boolean dispatchTouchEvent(final MotionEvent ev) {
        try {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                View v = getCurrentFocus();
                EdittorUtil.hideInput(this);
                return super.dispatchTouchEvent(ev);
            }
            if (getWindow().superDispatchTouchEvent(ev)) {
                return true;
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return onTouchEvent(ev);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (curriculumPresenter != null) {
            curriculumPresenter.onDestroy();
            curriculumPresenter = null;
        }
    }

}